package com.tiankong.product.event.listener;

import com.tiankong.product.dto.Bound;
import com.tiankong.product.dto.SpuInfoDto;
import com.tiankong.product.entity.SkuCoupon;
import com.tiankong.product.entity.SkuInfo;
import com.tiankong.product.event.ProductCouponEvent;
import com.tiankong.product.feign.CouponFeignClient;
import com.tiankong.product.feign.model.SpuCouponInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: scl
 * @date: 2021-07-20 15:31
 * @description 商品优惠信息变更监听器
 */
@Component
public class CouponListener {

    @Autowired
    private CouponFeignClient couponFeignClient;

    @EventListener(ProductCouponEvent.class)
    public void saveSpuCoupon(ProductCouponEvent event){

        SpuInfoDto spuInfo = event.spuInfo();
        //spu积分设置
        Bound bounds = spuInfo.getBounds();
        bounds.setSpuId(spuInfo.getId());
        List<SkuInfo> skus = spuInfo.getSkus();
        //sku折扣设置
        List<SkuCoupon> skuCoupons = skus.stream().map(s -> s.getCoupon().setSkuId(s.getSkuId()).setMemberPrice(s.getMemberPrice()))
                .collect(Collectors.toList());

        couponFeignClient.saveCoupon(new SpuCouponInfo().setBound(bounds).setSkuCoupon(skuCoupons));
    }
}
